package org.zjz.fun

import java.util.Objects
import com.fasterxml.jackson.annotation.JsonIgnore
import com.fasterxml.jackson.core.`type`.TypeReference
import com.fasterxml.jackson.module.scala.{DefaultScalaModule, ScalaObjectMapper}

class BattBean(var title: String, var titleType: String, var child: List[BattBean])  {
  @JsonIgnore
  private var index: Int = 0

  def this() {
    this(null, null, null)
  }

  override def toString = s"BattBean(title=$title, titleType=$titleType, child=$child)"

  def clearIndex: Unit = {
    index = 0
  }

   def hasNext: Boolean = {
    if (Objects.isNull(child)) {
     return false
    }
    if (index < child.size) {
      return true
    }
    false
  }

   def next(): BattBean = {
    if (hasNext) return child({
      //进行了赋值
      index += 1;
      //没有进行赋值直接返回
      index - 1
    })
    null
  }
}

object BattBean {

  import com.fasterxml.jackson.databind.ObjectMapper

  val objectMapper = new ObjectMapper() with ScalaObjectMapper

  def apply(json: String): List[BattBean] = {
    objectMapper.registerModule(DefaultScalaModule)
    var jsonNode = objectMapper.readTree(json).get("data")
    objectMapper.readValue(jsonNode.toPrettyString(), new TypeReference[List[BattBean]]() {})
  }
}